//
//  SAPSellCarViewController.m
//  CarsHome
//
//  Created by dapeng on 15/12/4.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "SAPSellCarViewController.h"

@interface SAPSellCarViewController ()<UIWebViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) SAPFindCarOneModel *oneModel;
@property (nonatomic, retain) SAPFindCarTwoModel *twoModel;
@property (nonatomic, retain) SAPFindCarThreeModel *threeModel;
@property (nonatomic, retain) UITableView *tableView;
@end

@implementation SAPSellCarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableViews];
    [self.view setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0]];
    [self.view addSubview:self.tableView];
    UIView *segView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    segView.backgroundColor = [UIColor whiteColor];
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"在售", @"全部"]];
    seg.frame = CGRectMake(10, 10, self.view.frame.size.width - 20, 30);
    seg.selectedSegmentIndex = 0;
    [segView addSubview:seg];
    [self.view addSubview:segView];
    self.titles = [[NSString alloc] init];
    
    [seg release];
    [segView release];
    [_tableView release];
    _HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _HUD.color = [UIColor blackColor];
    _HUD.dimBackground = YES;
    _HUD.labelText = @"正在加载";
    [SAPNetWorkTool getWithUrl:_url parameter:nil httpHeader:nil responseType:ResponseTypeJSON   success:^(id result) {
        NSDictionary *dic = [result valueForKey:@"result"];
        self.oneModel = [[SAPFindCarOneModel alloc] initWithDataSource:dic];
        _HUD.hidden = YES;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (void)tableViews {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[SAPSellCarTableViewCell class] forCellReuseIdentifier:@"SAPSellCarTableViewCellUITableViewIndentifier"];

    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.oneModel.fctlistArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    SAPFindCarTwoModel *two = _oneModel.fctlistArray[section];
    return two.serieslistArray.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    SAPFindCarTwoModel *two = _oneModel.fctlistArray[section];
    return two.name;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAPSellCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAPSellCarTableViewCellUITableViewIndentifier"];
    if (self.oneModel) {
        if (indexPath.section < _oneModel.fctlistArray.count) {
            _twoModel = self.oneModel.fctlistArray[indexPath.section];
        }
        if (indexPath.row < _twoModel.serieslistArray.count) {
            SAPFindCarThreeModel *threeModel = _twoModel.serieslistArray[indexPath.row];
            cell.threeModel = threeModel;
        }
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 80;
    }else {
        return 0;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];

    SAPFindCarThreeModel *threeModel = _twoModel.serieslistArray[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://comm.app.api.autohome.com.cn/static/sharejump.html?p=1&pm=1&go=1&cont=5&ourl=http://m.autohome.com.cn/%@/", threeModel.id];
    
    SAPSellDetailsViewController *detailsView = [[SAPSellDetailsViewController alloc] init];
    detailsView.url = str;
    detailsView.titles = threeModel.name;
    detailsView.prices = threeModel.prices;
    detailsView.picture = threeModel.imgurl;
    NSDictionary *dic = [[NSDictionary alloc] init];
    dic = [NSDictionary dictionaryWithObjectsAndKeys:threeModel.name, @"titles", threeModel.prices, @"prices", threeModel.imgurl, @"picture", str, @"ids", nil];
    detailsView.dictionary = dic;
    
   
    SAKeyValueStore *store = [[SAKeyValueStore alloc] initDBWithName:@"colle.db"];
    NSString *tableName = @"history_table";
    [store createTableWithName:tableName];
    [store putObject:dic withId:dic[@"titles"] intoTable:tableName];
    [self.navigationController pushViewController:detailsView animated:YES];
  
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
